﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Billing.CashDesk.AuthenticationService;
using Billing.CashDesk.CashDeskService;

namespace Billing.CashDesk
{
    internal static class ServiceFactory
    {
        private static NetworkCredential authenticationServiceNetworkCredential = null;
        private static NetworkCredential paymentPointServiceNetworkCredential = null;

        private static string authenticationServiceUrl = null;
        private static string cashDeskServiceUrl = null;

        public static string CashDeskServicekUrl
        {
            get
            {
                if (cashDeskServiceUrl == null)
                    cashDeskServiceUrl = SettingManager.getSetting("PaymentPointService");
                return cashDeskServiceUrl;
            }
        }
        public static string AuthenticationServiceUrl
        {
            get
            {
                if (authenticationServiceUrl == null)
                    authenticationServiceUrl = SettingManager.getSetting("AuthenticationService");
                return authenticationServiceUrl;
            }
        }
        
        internal static NetworkCredential PaymentPointServiceNetworkCredential
        {
            get
            {
                if (paymentPointServiceNetworkCredential == null)
                {
                    paymentPointServiceNetworkCredential = new NetworkCredential(
                    SettingManager.getSetting("PaymentPointServiceUser"),
                    SettingManager.getSetting("PaymentPointServicePassword"));
                }
                return paymentPointServiceNetworkCredential;
            }
        }

        internal static IAuthenticationService CreateAuthenticationService()
        {
            IAuthenticationService result = new IAuthenticationService();

            result.Url = AuthenticationServiceUrl;
            result.UseDefaultCredentials = false;

            result.Credentials = PaymentPointServiceNetworkCredential;
            result.BillingSoapHeaderValue = new Billing.CashDesk.AuthenticationService.BillingSoapHeader();

            result.BillingSoapHeaderValue.UserName = Profile.UserName;
            result.BillingSoapHeaderValue.PasswordHash = Profile.PasswordHash;
            result.BillingSoapHeaderValue.PaymentPointID = CashDeskSettings.PaymentPointID;
            result.BillingSoapHeaderValue.PaymentPorintName = CashDeskSettings.PaymentPointName;
            result.BillingSoapHeaderValue.HostAddress = Billing.Net.NetInfo.HostNameOrIPAddress;

            return result;
        }

        internal static ICashDeskService CreateCashDeskService()
        {
            ICashDeskService result = new ICashDeskService();

            result.Url = CashDeskServicekUrl;
            result.UseDefaultCredentials = false;

            result.Credentials = PaymentPointServiceNetworkCredential;

            result.BillingSoapHeaderValue = new Billing.CashDesk.CashDeskService.BillingSoapHeader();

            result.BillingSoapHeaderValue.UserName = Profile.UserName;
            result.BillingSoapHeaderValue.PasswordHash = Profile.PasswordHash;
            result.BillingSoapHeaderValue.PaymentPointID = CashDeskSettings.PaymentPointID;
            result.BillingSoapHeaderValue.PaymentPorintName = CashDeskSettings.PaymentPointName;
            result.BillingSoapHeaderValue.HostAddress = Billing.Net.NetInfo.HostNameOrIPAddress;

            return result;
        }
    }
}
